﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using STLGROUP.Layers.Presentations.FormBase;
using STLGROUP.Layers.DataAccessLayers;
using QuanLyBanHang.SystemForms;
using STLGROUP.Layers.Common;

namespace QuanLyBanHang.SystemForms
{
    /// <summary>
    /// Form login
    /// </summary>
    /// <author>viet sang</author>
    /// 
    public partial class frmLogin : frmLoginBase
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            txtUserName.Focus();
        }

        private void btnLogin_Click_1(object sender, EventArgs e)
        {
            lblReminder.Text = string.Empty;

            if (string.IsNullOrWhiteSpace(txtUserName.Text))
            {
                return;
            }

            ACL_TaiKhoan rootUser = new ACL_TaiKhoan() {IDTaiKhoan=0, TenDangNhap="root", MatKhau="qlbh100", IDNhomNguoiDung=1};

            //If userRoot
            if (txtUserName.Text == rootUser.TenDangNhap)
            {
                if (txtPassword.Text == rootUser.MatKhau)
                {
                    UICommon.CurrentUserInfo = rootUser;
                    this.Close();
                }
            }
            else
            {
                try
                {
                    //ACL_TaiKhoan taiKhoan = taiKhoan = Cache.Context.ACL_TaiKhoan.Where(s => s.TenDangNhap.ToUpper() == txtUserName.Text.ToUpper()).FirstOrDefault();
                    ACL_TaiKhoan taiKhoan  = (from tk in Cache.Context.ACL_TaiKhoan
                                             join nv in Cache.Context.DT_NhanVien on tk.IDNhanVien equals nv.IDNhanVien
                                             where tk.TenDangNhap.ToUpper()==txtUserName.Text.ToUpper()
                                             && nv.IDTrangThaiNhanVien == (int)StructsEnums.EnumTrangThaiNhanVien.DangLamViec
                                             select tk).FirstOrDefault();

                    if (taiKhoan != null)
                    {
                        if (UICommon.Decoding(txtPassword.Text.Trim(), taiKhoan.MatKhau.Trim()))
                        {
                            //Dang nhap lan cuoi
                            taiKhoan.DangNhapCuoi = UICommon.GetDateTime();
                            Cache.Context.SaveChanges();

                            //get currentUserInfo
                            UICommon.CurrentUserInfo = taiKhoan;

                            //close form
                            this.Close();
                        }
                        else
                        {
                            UICommon.ShowMessegeInfo(Const.MSG_PASSWORD_KHONG_DUNG);
                            lblReminder.Text = "Gợi ý mật khẩu: "+ taiKhoan.GoiYMatKhau;
                            return;
                        }
                    }
                    else
                    {
                        UICommon.ShowMessegeInfo(Const.MSG_LOGIN_FAILE_CHECK_USER);
                        return;
                    }
                }
                catch
                {
                    UICommon.ShowMessegeInfo(Const.MSG_LOGIN_FAILE_CHECK_USER);
                }
            }
        }

        private void btnCaiDatKetNoi_Click(object sender, EventArgs e)
        {
            frmConfiguration frm = new frmConfiguration();
            frm.ShowDialog();
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            try
            {
                string path = Application.StartupPath + @"\HuongDanSuDung\HuongDanSuDungQLBH.pdf";
                System.Diagnostics.Process.Start(path);
            }
            catch
            {
                MessageBox.Show("Không thể mở trợ giúp. Vui lòng kiểm tra lại.");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

       
    }
}
